//
//  RegisterAndLoginVC.m
//  MiTu
//
//  Created by CY on 15/11/11.
//  Copyright © 2015年 CY. All rights reserved.
//

#import "RegisterAndLoginVC.h"
#import "PersonalInfoVC.h"
#import "UMSocial.h"

#define CYCLE_IMAGE_COUNT 3

@interface RegisterAndLoginVC ()<UITextFieldDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) UIView * topView; // 轮播图上面的部分
@property (nonatomic, strong) UIImageView * iconIV; // topView中间的图标
@property (nonatomic, strong) UIView * middleView;
@property (nonatomic, strong) UIView * bottomView; // 从“————or————”开始下面的部分

@property (nonatomic, strong) UIView * cyclePageView; // 轮播图
@property (nonatomic, strong) UIScrollView * cyclePageSV;
@property (nonatomic, strong) UIPageControl * pageControl;

@property (nonatomic, strong) UIButton * registerBtn;
@property (nonatomic, strong) UIButton * loginBtn;
@property (nonatomic, strong) UIButton * confirmBtn;
@property (nonatomic, assign) BOOL loginClicked; // 判断当前选择的是否为登录按钮

@property (nonatomic, strong) UITextField * textField1; // +86
@property (nonatomic, strong) UITextField * textField2; // 手机号（或昵称）
@property (nonatomic, strong) UITextField * textField3; // 密码
@property (nonatomic, strong) UIButton * loginConfirmedBtn; // 确定登录按钮

@property (nonatomic, strong) NSString * userName;
@property (nonatomic, strong) NSString * password;

@property (nonatomic, strong) UILabel * remindLbl1; // 请输入正确的手机号 / 该手机号已被注册
@property (nonatomic, strong) UILabel * remindLbl2; // 密码不能为空

@property (nonatomic, strong) UIView * leftGapLine;
@property (nonatomic, strong) UILabel * orLabel;
@property (nonatomic, strong) UIView * rightGapLine;

// 三方登录按钮
@property (nonatomic, strong) UIButton * weixinBtn;
@property (nonatomic, strong) UIButton * qqBtn;
@property (nonatomic, strong) UIButton * weiboBtn;

@property (nonatomic, strong) NSTimer *timer; // 定时器

@end

@implementation RegisterAndLoginVC

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self preferredStatusBarStyle];
    [self setUI];
    [self setupConstraints];
    
}

- (void)setUI
{
    [self.view addSubview:self.middleView];
    [self.view addSubview:self.topView];
    [self.view addSubview:self.bottomView];
}

- (void)setupConstraints
{
    NSDictionary * variableBindings = NSDictionaryOfVariableBindings(_topView, _middleView, _bottomView, _iconIV, _leftGapLine, _rightGapLine, _orLabel, _weiboBtn, _weixinBtn, _qqBtn, _registerBtn, _loginBtn);
    // topView & middleView & bottomView
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_topView]-0-|" options:0 metrics:nil views:variableBindings]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_middleView]-0-|" options:0 metrics:nil views:variableBindings]];
    if (ISIPHONE3_5) {
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_topView(140)]-0-[_middleView(397)]" options:0 metrics:nil views:variableBindings]];
    } else {
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_topView(height1)]-0-[_middleView(height2)]" options:0 metrics:@{@"height1":@([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:196.f]),@"height2":@([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:659.f])} views:variableBindings]];
    }
    if (ISIPHONE3_5) {
        [MTConstraintHelper setView:self.bottomView fullAsSuperview:self.view andEdgeInsets:UIEdgeInsetsMake([MTSize getLengthWithSizeType:OTSSizeType3_5 andLength:407.f], 0, 0, 0)];
    } else {
        [MTConstraintHelper setView:self.bottomView fullAsSuperview:self.view andEdgeInsets:UIEdgeInsetsMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:582.f], 0, 0, 0)];
    }
    // iconIV
    if (ISIPHONE3_5) {
        [self.topView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(40)-[_iconIV(85)]" options:0 metrics:nil views:variableBindings]];
        [self.topView addConstraint:[NSLayoutConstraint constraintWithItem:self.iconIV attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.iconIV attribute:NSLayoutAttributeWidth multiplier:1.0 constant:-10]];
    } else {
        [self.topView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(gap1)-[_iconIV(height)]" options:0 metrics:@{@"gap1":@([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:67.f]),@"height":@([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:105.f])} views:variableBindings]];
    }
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.topView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:_iconIV attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    
    // 轮播图
    if (ISIPHONE3_5) {
        [MTConstraintHelper setView:self.cyclePageView fullAsSuperview:self.middleView andEdgeInsets:UIEdgeInsetsMake(0, 0, 226, 0)];
    } else {
        [MTConstraintHelper setView:self.cyclePageView fullAsSuperview:self.middleView andEdgeInsets:UIEdgeInsetsMake(0, 0, [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:386], 0)];
    }
    [self setCyclePageView];
    
    // 注册&登录按钮
    if (ISIPHONE3_5) {
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(88)-[_registerBtn]-(82)-[_loginBtn]" options:NSLayoutFormatAlignAllCenterY metrics:nil views:variableBindings]];
        [self.middleView addConstraint:[NSLayoutConstraint constraintWithItem:self.cyclePageView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.registerBtn attribute:NSLayoutAttributeTop multiplier:1.0 constant:-10]];
    } else {
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(width1)-[_registerBtn]-(width2)-[_loginBtn]" options:NSLayoutFormatAlignAllCenterY metrics:@{@"width1":@([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:100]),@"width2":@([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:103.5])} views:variableBindings]];
        [self.middleView addConstraint:[NSLayoutConstraint constraintWithItem:self.cyclePageView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.registerBtn attribute:NSLayoutAttributeTop multiplier:1.0 constant:[MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:-18]]];
    }
    
    // 分割线
    [self.bottomView addConstraint:[NSLayoutConstraint constraintWithItem:self.leftGapLine attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.leftGapLine attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.5]];
    [self.bottomView addConstraint:[NSLayoutConstraint constraintWithItem:self.rightGapLine attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.rightGapLine attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.5]];
    [self.bottomView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_leftGapLine(length)]-(6)-[_orLabel]-(6)-[_rightGapLine(length)]" options:NSLayoutFormatAlignAllCenterY metrics:@{@"length":@([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:89.f])} views:variableBindings]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.qqBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.orLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:8]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.bottomView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.orLabel attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    
//     weixinBtn & qqBtn & weiboBtn
    [self.bottomView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(gap1)-[_weixinBtn(width)]-(gap2)-[_qqBtn(width)]-(gap2)-[_weiboBtn(width)]-(gap1)-|" options:NSLayoutFormatAlignAllCenterY metrics:@{@"gap1":@([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:86.f]),@"gap2":@([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:61.f]),@"width":@([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:27.f])} views:variableBindings]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.bottomView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.weixinBtn attribute:NSLayoutAttributeBottom multiplier:1.0 constant:[MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:35.f]]];
}

- (void)setCyclePageView
{
    [self.cyclePageView addSubview:self.cyclePageSV];
    [self.cyclePageView addSubview:self.pageControl];
    [self addTimer];
}

#pragma mark - Property
- (UIView *)topView
{
    if (!_topView) {
        _topView = [UIView autolayoutView];
        _topView.backgroundColor = [MTColor colorWithRGB:0xF8B62C];
        self.iconIV = [UIImageView autolayoutView];
        self.iconIV.image = [UIImage imageNamed:@"me_too"];
        [_topView addSubview:self.iconIV];
    }
    return _topView;
}

- (UIView *)middleView
{
    if (!_middleView) {
        _middleView = [UIView autolayoutView];
        
        [_middleView addSubview:self.cyclePageView];
        [_middleView addSubview:self.registerBtn];
        [_middleView addSubview:self.loginBtn];

        _textField1 = [[UITextField alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:86], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:273+62], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:40], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:31])];
        _textField1.delegate = self;
        _textField1.borderStyle = UITextBorderStyleRoundedRect;
        _textField1.text = @"+86";
        _textField1.textColor = [UIColor grayColor];
        _textField1.font = [UIFont systemFontOfSize:[MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:11]];
        [_middleView addSubview:_textField1];
        
        _textField2 = [[UITextField alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:130], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:273+62], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:150], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:31])];
        _textField2.delegate = self;
        _textField2.borderStyle = UITextBorderStyleRoundedRect;
        _textField2.placeholder = @"输入手机号";
        
        _textField2.text = @"12345678900";
        
        _textField2.textColor = [UIColor grayColor];
        _textField2.tintColor = [UIColor lightGrayColor];
        _textField2.font = [UIFont systemFontOfSize:12];
        [_middleView addSubview:_textField2];
        
        _remindLbl1 = [[UILabel alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:0], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:366], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:150], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:20])];
        _remindLbl1.right = [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:278];
        _remindLbl1.textColor = [UIColor redColor];
        _remindLbl1.textAlignment = NSTextAlignmentRight;
        _remindLbl1.font = [UIFont systemFontOfSize:[MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:10.f]];
        _remindLbl1.hidden = YES;
        [_middleView addSubview:_remindLbl1];
        
        _confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:280], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:273+62], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:50], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:31])];
        [_confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_confirmBtn setTitleColor:[MTColor colorWithRGB:0x888889] forState:UIControlStateNormal];
        [_confirmBtn setTitleColor:[MTColor colorWithRGB:0xF8B62C] forState:UIControlStateHighlighted];
        _confirmBtn.titleLabel.font = [UIFont systemFontOfSize:[MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:15]];
        [_confirmBtn addTarget:self action:@selector(confirmBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_middleView addSubview:_confirmBtn];
        
        _textField3 = [[UITextField alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:86], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:273+62+60], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:194], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:31])];
        _textField3.delegate = self;
        _textField3.borderStyle = UITextBorderStyleRoundedRect;
        _textField3.placeholder = @"输入密码";
        _textField3.secureTextEntry = YES;
        _textField3.textColor = [UIColor grayColor];
        _textField3.tintColor = [UIColor lightGrayColor];
        _textField3.font = [UIFont systemFontOfSize:12];
        UIView * leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 24, 20)];
        UIImageView * leftIV = [[UIImageView alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:8], 0, [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:16], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:20])];
        leftIV.image = [UIImage imageNamed:@"password"];
        [leftView addSubview:leftIV];
        _textField3.leftView = leftView;
        _textField3.leftViewMode = UITextFieldViewModeAlways;
        [_middleView addSubview:_textField3];
        
        _remindLbl2 = [[UILabel alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:0], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:426], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:150], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:20])];
        _remindLbl2.right = [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:278];
        _remindLbl2.text = @"账号或密码有误";
        _remindLbl2.textColor = [UIColor redColor];
        _remindLbl2.textAlignment = NSTextAlignmentRight;
        _remindLbl2.font = [UIFont systemFontOfSize:[MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:10.f]];
        _remindLbl2.hidden = YES;
        [_middleView addSubview:_remindLbl2];
        
        _loginConfirmedBtn = [[UIButton alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:86], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:273+62+60+60], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:194], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:31])];
        [_loginConfirmedBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_loginConfirmedBtn setBackgroundImage:[UIImage imageWithColor:[MTColor colorWithRGB:0xEECE96]] forState:UIControlStateNormal];
        [_loginConfirmedBtn setBackgroundImage:[UIImage imageWithColor:[MTColor colorWithRGB:0xF8B62C]] forState:UIControlStateHighlighted];
        _loginConfirmedBtn.titleLabel.font = [UIFont systemFontOfSize:[MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:15]];
        _loginConfirmedBtn.layer.masksToBounds = YES;
        _loginConfirmedBtn.layer.cornerRadius = 5;
        [_loginConfirmedBtn addTarget:self action:@selector(loginConfirmBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_middleView addSubview:_loginConfirmedBtn];
        
        if (ISIPHONE3_5) {
            _textField1.frame = CGRectMake(66, 222, 40, 28);
            _textField2.frame = CGRectMake(110, 222, 140, 28);
            _confirmBtn.frame = CGRectMake(245, 222, 50, 28);
            _remindLbl1.frame = CGRectMake(0, 250, 120, 18);
            _remindLbl1.right = 250;
            _remindLbl1.font = [UIFont systemFontOfSize:9];
            _textField3.frame = CGRectMake(66, 270, 184, 28);
            _remindLbl2.frame = CGRectMake(0, 298, 120, 18);
            _remindLbl2.right = 250;
            _remindLbl2.font = [UIFont systemFontOfSize:9];
            _loginConfirmedBtn.frame = CGRectMake(66, 330, 184, 28);
            
            _registerBtn.titleLabel.font = [UIFont systemFontOfSize:16];
            _loginBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        }
    }
    return _middleView;
}

- (UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [UIView autolayoutView];
        _bottomView.backgroundColor = [UIColor whiteColor];
        [_bottomView addSubview:self.leftGapLine];
        [_bottomView addSubview:self.orLabel];
        [_bottomView addSubview:self.rightGapLine];
        [_bottomView addSubview:self.weixinBtn];
        [_bottomView addSubview:self.qqBtn];
        [_bottomView addSubview:self.weiboBtn];
    }
    return _bottomView;
}

- (UIView *)cyclePageView
{
    if (!_cyclePageView) {
        CGFloat h = 273;
        if (ISIPHONE3_5) {
            h = 200;
        }
        _cyclePageView = [[UIView alloc] initWithFrame:CGRectMake(0, [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:196], UI_SCREEN_WIDTH, [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:h])];
        _cyclePageView.backgroundColor = [UIColor clearColor];
    }
    return _cyclePageView;
}

- (UIScrollView *)cyclePageSV
{
    if (!_cyclePageSV) {
        CGFloat h = 273;
        if (ISIPHONE3_5) {
            h = 200;
        }
        
        _cyclePageSV = [[UIScrollView alloc] initWithFrame:self.cyclePageView.bounds];
        _cyclePageSV.delegate = self;
        _cyclePageSV.contentSize = CGSizeMake(CYCLE_IMAGE_COUNT * UI_SCREEN_WIDTH, [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:h]);
        _cyclePageSV.backgroundColor = [UIColor clearColor];
        _cyclePageSV.showsHorizontalScrollIndicator = NO;
        _cyclePageSV.pagingEnabled = YES;
        
        NSArray * imageUrlArr = @[IMAGE_URL_1, IMAGE_URL_2, IMAGE_URL_3];
        for (int i=0; i<CYCLE_IMAGE_COUNT; i++) {
            UIImageView * iv = [[UIImageView alloc] init];
            [iv sd_setImageWithURL:[NSURL URLWithString:imageUrlArr[i]]];
            iv.frame = CGRectMake(i * UI_SCREEN_WIDTH, 0, UI_SCREEN_WIDTH, [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:h]);
            [_cyclePageSV addSubview:iv];
        }
    }
    return _cyclePageSV;
}

- (UIPageControl *)pageControl
{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, _cyclePageSV.bottom - 20, UI_SCREEN_WIDTH, 20)];
        _pageControl.numberOfPages = CYCLE_IMAGE_COUNT;
        
    }
    return _pageControl;
}

- (UIButton *)registerBtn
{
    if (!_registerBtn) {
        _registerBtn = [UIButton autolayoutView];
        _registerBtn.tag = 20;
        [_registerBtn setTitle:@"注册" forState:UIControlStateNormal];
        [_registerBtn setTitleColor:[MTColor colorWithRGB:0xF8B62C] forState:UIControlStateNormal];
        _registerBtn.titleLabel.font = [UIFont systemFontOfSize:[MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:18]];
        [_registerBtn addTarget:self action:@selector(registerOrLoginClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registerBtn;
}

- (UIButton *)loginBtn
{
    if (!_loginBtn) {
        _loginBtn = [UIButton autolayoutView];
        _loginBtn.tag = 21;
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_loginBtn setTitleColor:[MTColor colorWithRGB:0x888889] forState:UIControlStateNormal];
        _loginBtn.titleLabel.font = [UIFont systemFontOfSize:[MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:18]];
        [_loginBtn addTarget:self action:@selector(registerOrLoginClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginBtn;
}

- (UIView *)leftGapLine
{
    if (!_leftGapLine) {
        _leftGapLine = [UIView autolayoutView];
        _leftGapLine.backgroundColor = [MTColor colorWithRGB:0x888889];
    }
    return _leftGapLine;
}

- (UILabel *)orLabel
{
    if (!_orLabel) {
        _orLabel = [UILabel autolayoutView];
        _orLabel.text = @"or";
        _orLabel.textColor = [MTColor colorWithRGB:0x888889];
        _orLabel.font = [UIFont systemFontOfSize:[MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:14.f]];
    }
    return _orLabel;
}

- (UIView *)rightGapLine
{
    if (!_rightGapLine) {
        _rightGapLine = [UIView autolayoutView];
        _rightGapLine.backgroundColor = [MTColor colorWithRGB:0x888889];
    }
    return _rightGapLine;
}

- (UIButton *)weixinBtn
{
    if (!_weixinBtn) {
        _weixinBtn = [UIButton autolayoutView];
        [_weixinBtn setImage:[UIImage imageNamed:@"weixinBtn"] forState:UIControlStateNormal];
        [_weixinBtn addTarget:self action:@selector(weixinBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _weixinBtn;
}

- (UIButton *)qqBtn
{
    if (!_qqBtn) {
        _qqBtn = [UIButton autolayoutView];
        [_qqBtn setImage:[UIImage imageNamed:@"qqBtn"] forState:UIControlStateNormal];
        [_qqBtn addTarget:self action:@selector(qqBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _qqBtn;
}

- (UIButton *)weiboBtn
{
    if (!_weiboBtn) {
        _weiboBtn = [UIButton autolayoutView];
        [_weiboBtn setImage:[UIImage imageNamed:@"weiboBtn"] forState:UIControlStateNormal];
        [_weiboBtn addTarget:self action:@selector(weiboBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _weiboBtn;
}

#pragma mark - Action
- (void)registerOrLoginClicked:(UIButton *)button
{
    if (button.tag == 20 && self.loginClicked == 1) {
        self.loginClicked = !self.loginClicked;
        self.textField2.text = @"";
        self.textField3.text = @"";
        self.password = @"";
        self.remindLbl1.hidden = YES;
        self.remindLbl2.hidden = YES;
        [self.registerBtn setTitleColor:[MTColor colorWithRGB:0xF8B62C] forState:UIControlStateNormal];
        [self.loginBtn setTitleColor:[MTColor colorWithRGB:0x888889] forState:UIControlStateNormal];
        if (ISIPHONE3_5) {
            [UIView animateWithDuration:0.6 animations:^{
                CGRect frame = self.middleView.frame;
                self.middleView.bounds = CGRectMake(frame.origin.x, 0, frame.size.width, frame.size.height);
                _textField2.placeholder = @"输入手机号";
                _confirmBtn.hidden = NO;
            } completion:nil];
        } else {
            [UIView animateWithDuration:0.6 animations:^{
                CGRect frame = self.middleView.frame;
                self.middleView.bounds = CGRectMake(frame.origin.x, 0, frame.size.width, frame.size.height);
                _textField2.placeholder = @"输入手机号";
                _confirmBtn.hidden = NO;
            } completion:nil];
        }
    } else if (button.tag == 21 && self.loginClicked == 0) {
        self.loginClicked = !self.loginClicked;
        self.textField2.text = @"";
        self.remindLbl1.hidden = YES;
        [self.registerBtn setTitleColor:[MTColor colorWithRGB:0x888889] forState:UIControlStateNormal];
        [self.loginBtn setTitleColor:[MTColor colorWithRGB:0xF8B62C] forState:UIControlStateNormal];
        if (ISIPHONE3_5) {
            [UIView animateWithDuration:0.6 animations:^{
                CGRect frame = self.middleView.frame;
                self.middleView.bounds = CGRectMake(frame.origin.x, 171, frame.size.width, frame.size.height);
                _textField2.placeholder = @"输入手机号或昵称";
                _confirmBtn.hidden = YES;
            } completion:nil];
        } else {
            [UIView animateWithDuration:0.6 animations:^{
                CGRect frame = self.middleView.frame;
                self.middleView.bounds = CGRectMake(frame.origin.x, [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:273], frame.size.width, frame.size.height);
                _textField2.placeholder = @"输入手机号或昵称";
                _confirmBtn.hidden = YES;
            } completion:nil];
        }
    }
}

- (void)confirmBtnClicked:(UIButton *)button
{
    if ([self judgeRegisterPhoneNum:self.textField2.text]) { // 是否是11位，是否以1开头
        AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
        WEAK_SELF;
        [manager GET:[NSString stringWithFormat:REGISTER_VERIFYPHONE,self.textField2.text] parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            STRONG_SELF;
            if (responseObject[@"success"]) {
                self.remindLbl1.hidden = YES;
                PersonalInfoVC * vc = [[PersonalInfoVC alloc] init];
                vc.registerPhoneNum = self.textField2.text;
                [self presentViewController:vc animated:NO completion:nil];
            } else {
                self.remindLbl1.text = @"该账号已被注册";
                self.remindLbl1.hidden = NO;
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@",error.localizedDescription);
            self.remindLbl1.text = @"未能连接到服务器";
            self.remindLbl1.hidden = NO;
        }];
    } else {
        self.remindLbl1.text = @"请输入正确的手机号";
        self.remindLbl1.hidden = NO;
    }
}

- (void)loginConfirmBtnClicked:(UIButton *)button
{
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    WEAK_SELF;
    [manager GET:[NSString stringWithFormat:LOGIN,self.textField2.text,[self.textField3.text stringFromMD5]] parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        STRONG_SELF; // 登录成功 密码错误
        if (![responseObject[@"success"] boolValue] && [responseObject[@"message"] isEqualToString:@"该帐号未注册"]) {
            self.remindLbl1.text = @"该账号未注册";
            self.remindLbl1.hidden = NO;
            self.remindLbl2.hidden = YES;
        } else if (![responseObject[@"success"] boolValue] && [responseObject[@"message"] isEqualToString:@"密码错误"]) {
            self.remindLbl2.text = @"用户名或密码错误";
            self.remindLbl2.hidden = NO;
            self.remindLbl1.hidden = YES;
        } else if ([responseObject[@"success"] boolValue]) {
            // 登录成功，跳转到首页
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}

- (void)weixinBtnClicked:(UIButton *)button
{
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary]valueForKey:UMShareToWechatSession];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
        }
    });
}

- (void)qqBtnClicked:(UIButton *)button
{
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        // 获取QQ用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToQQ];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            
            AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
            [manager GET:[NSString stringWithFormat:THIRDLOGIN,snsAccount.usid,2] parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
                NSLog(@"123");
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                NSLog(@"%@",error.localizedDescription);
            }];
        }});
}

- (void)weiboBtnClicked:(UIButton *)button
{
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
            
            NSLog(@"response is %@",response);
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            
            AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
            [manager GET:[NSString stringWithFormat:THIRDLOGIN,snsAccount.usid,3] parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
                NSLog(@"123");
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                NSLog(@"%@",error.localizedDescription);
            }];
            
        }});
}

#pragma mark - UIScrollViewDelegate
/**
 *  当scrollView正在滚动就会调用
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 根据scrollView的滚动位置决定pageControl显示第几页
    CGFloat scrollW = scrollView.frame.size.width;
    int page = (scrollView.contentOffset.x + scrollW * 0.5) / scrollW;
    self.pageControl.currentPage = page;
}

/**
 *  开始拖拽的时候调用
 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // 停止定时器(一旦定时器停止了,就不能再使用)
    [self removeTimer];
}

/**
 *  停止拖拽的时候调用
 */
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    // 开启定时器
    [self addTimer];
}

#pragma mark - 轮播图
/**
 *  添加定时器
 */
- (void)addTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

/**
 *  移除定时器
 */
- (void)removeTimer
{
    [self.timer invalidate];
    self.timer = nil;
}

- (void)nextImage
{
    // 1.增加pageControl的页码
    NSInteger page = 0;
    if (self.pageControl.currentPage == CYCLE_IMAGE_COUNT - 1) {
        page = 0;
    } else {
        page = self.pageControl.currentPage + 1;
    }
    
    // 2.计算scrollView滚动的位置
    CGFloat offsetX = page * self.cyclePageSV.frame.size.width;
    CGPoint offset = CGPointMake(offsetX, 0);
    [self.cyclePageSV setContentOffset:offset animated:YES];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField == _textField1) {
        return NO;
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == _textField2 && self.loginClicked == NO) {
        [_textField2 resignFirstResponder];
        [self confirmBtnClicked:nil];
    } else if (textField == _textField2 && self.loginClicked == YES) {
        if ([textField.text isEqualToString:@""]) {
            _remindLbl1.text = @"账号不能为空";
            _remindLbl1.hidden = NO;
            _remindLbl2.hidden = YES;
        } else {
            self.userName = textField.text;
            _remindLbl1.hidden = YES;
            [_textField2 resignFirstResponder];
            [_textField3 becomeFirstResponder];
        }
    } else if (textField == _textField3) {
        [_textField3 resignFirstResponder];
        self.password = textField.text;
        [self loginConfirmBtnClicked:nil];
    }
    return YES;
}

#pragma mark - Help
- (BOOL)judgeRegisterPhoneNum:(NSString *)string
{
    if (string.length == 11) {
        if ([string hasPrefix:@"1"]) {
            for (int i=0; i<string.length; i++) {
                if ([string characterAtIndex:i]>='0' && [string characterAtIndex:i]<='9') {
                    if (i == 10) {
                        return YES;
                    }
                }
            }
        }
    }
    return NO;
}

@end





